import { resolve } from 'path'
import { defineConfig, externalizeDepsPlugin, loadEnv, type UserConfig } from 'electron-vite'
import vue from '@vitejs/plugin-vue'
import Components from 'unplugin-vue-components/vite'
import AutoImport from 'unplugin-auto-import/vite'
import UnoCSS from 'unocss/vite'

export default defineConfig(({ mode }): UserConfig => {
  const env = loadEnv(mode, process.cwd())
  return {
    main: {
      resolve: {
        alias: {
          '@renderer': resolve('src/renderer/src'),
          '@main': resolve('src/main'),
          '@ipc': resolve('src/ipc'),
          '@config': resolve('config')
        }
      },
      plugins: [externalizeDepsPlugin()]
    },
    preload: {
      resolve: {
        alias: {
          '@renderer': resolve('src/renderer/src'),
          '@main': resolve('src/main'),
          '@ipc': resolve('src/ipc')
        }
      },
      plugins: [externalizeDepsPlugin()]
    },
    renderer: {
      server: {
        port: Number(env.VITE_PORT)
      },
      resolve: {
        alias: {
          '@renderer': resolve('src/renderer/src'),
          '@main': resolve('src/main'),
          '@ipc': resolve('src/ipc')
        }
      },
      plugins: [
        vue(),
        UnoCSS(),
        // 自动导入 components 下面的组件，无需 import 引入
        Components({
          deep: true,
          extensions: ['vue'],
          dirs: ['src/components'], // 自动导入的组件目录
          dts: 'src/types/components.d.ts' // 指定类型声明文件的路径
        }),
        AutoImport({
          imports: ['vue', 'vue-router', '@vueuse/core', 'pinia'],
          dts: 'src/types/auto-imports.d.ts',
          eslintrc: {
            // 这里先设置成true然后pnpm dev 运行之后会生成 .auto-import.json 文件之后，在改为false
            enabled: true,
            filepath: './.auto-import.json',
            globalsPropValue: true
          }
        })
      ]
    }
  }
})
